//
//  PhoneSmsLoginView.m
//  BaseProject
//
//  Created by 王洋 on 2021/1/20.
//  Copyright © 2021 王洋. All rights reserved.
//

#import "PhoneSmsLoginView.h"

@implementation PhoneSmsLoginView

+ (CGFloat)calculateHeight {
    return 110;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    
    UILabel *lab = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 60, 40)];
    lab.textColor = UIColor.whiteColor;
    lab.text = @"+86";
    lab.textAlignment = NSTextAlignmentCenter;
    lab.font = [UIFont systemFontOfSize:16];
    
    UIView *leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 60, 40)];
    leftView.backgroundColor = [UIColor clearColor];
    [leftView addSubview:lab];
    
    self.phoneTF.leftView =leftView;
    self.phoneTF.leftViewMode = UITextFieldViewModeAlways;
    
    NSAttributedString *attrString = [[NSAttributedString alloc] initWithString:@"请输入手机号" attributes:
    @{NSForegroundColorAttributeName:UIColorHex(0xd8d8d8),
                 NSFontAttributeName:self.phoneTF.font
         }];
    self.phoneTF.attributedPlaceholder = attrString;
    
    
    WEAKSELF
    [[RACSignal merge:@[self.phoneTF.rac_textSignal, RACObserve(self.phoneTF, text)]] subscribeNext:^(NSString* text){

        if (weakSelf.phoneTF.text.length == 11 && [Utilities checkPhoneTel:weakSelf.phoneTF.text]) {
            [weakSelf.btnBGImageView setImage:[UIImage imageNamed:@"rect_orange"]];
            weakSelf.btnBGImageView.alpha = 1;
            [weakSelf.handleBtn setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
            weakSelf.handleBtn.enabled = YES;
        } else {
            [weakSelf.btnBGImageView setImage:[UIImage imageNamed:@""]];
            weakSelf.btnBGImageView.alpha = 0.8;
            [weakSelf.handleBtn setTitleColor:UIColorHex(0x777777) forState:UIControlStateNormal];
            weakSelf.handleBtn.enabled = NO;
        }
    }];
    
    
    
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.01 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        [self.btnBGImageView pp_clipCorners:UIRectCornerAllCorners radius:self.btnBGImageView.frame.size.height/2];
        
    });
}

- (IBAction)handleAction:(id)sender {
    if (self.PhoneSmsLoginViewBlock) {
        self.PhoneSmsLoginViewBlock(PhoneSmsActionType_getCode);
    }
}


@end
